package com.sb.framework.util;

import javax.servlet.ServletContext;

import org.apache.log4j.Logger;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanNameAware;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.web.context.ServletContextAware;

import com.sb.framework.exception.GetBeanException;

public class WebUtil implements ApplicationContextAware, BeanNameAware, ServletContextAware {

	private static final Logger			logger			= Logger.getLogger( WebUtil.class );
	private static ApplicationContext	apt				= null;
	private static ServletContext		servletContext	= null;

	public static Object getBean(String beanName) {
		Object obj = null;
		try {
			logger.debug( "DEBUG===>GetBean  beanName=" + beanName );
			obj = apt.getBean( beanName );
		} catch (Exception e) {
			throw new GetBeanException();
		}
		return obj;
	}

	public static ApplicationContext getSpringContext() {
		return apt;
	}

	public static ServletContext getServletContext() {
		return servletContext;
	}

	@Override
	public void setServletContext(ServletContext servletContext) {
		WebUtil.servletContext = servletContext;
	}

	@Override
	public void setBeanName(String name) {
	}

	@Override
	public void setApplicationContext(ApplicationContext paramApplicationContext) throws BeansException {
		apt = paramApplicationContext;
	}

}
